package io.renren.service.impl;

import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import io.renren.dao.SysProductDao;
import io.renren.dao.SysRetailDao;
import io.renren.entity.SysProductEntity;
import io.renren.entity.SysRetailEntity;
import io.renren.service.SysRetailService;
import io.renren.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;



@Service("sysRetailService")
public class SysRetailServiceImpl extends ServiceImpl<SysRetailDao, SysRetailEntity> implements SysRetailService {

    @Autowired
    private SysProductDao sysProductDao;

    @Autowired
    private SysRetailDao sysRetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRetailEntity> page = this.page(
                new Query<SysRetailEntity>().getPage(params),
                new QueryWrapper<SysRetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils inventories(Integer retailId, Map<String, Object> params) {
        IPage<SysProductEntity> page = new Query<SysProductEntity>().getPage(params);
        List<SysProductEntity> products =  sysProductDao.retailInventories(retailId);
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(products);
        return pageUtils;
    }

    @Override
    public PageUtils queryByProductAndSubRetail(Integer productId, Integer level, Map<String, Object> params) {
        List<Integer> ids = sysProductDao.queryByProductAndSubRetail(productId, level - 1);
        IPage<SysRetailEntity> page = new LambdaQueryChainWrapper<>(sysRetailDao)
                .in(SysRetailEntity::getId, ids)
                .page(new Query<SysRetailEntity>().getPage(params));

        return new PageUtils(page);
    }


}
